package com.gp.pj.controller;

import com.alibaba.fastjson.JSONObject;
import com.gp.pj.service.UserService;
import com.gp.pj.utils.JWTUtils;
import com.gp.pj.utils.ResultCode;
import com.gp.pj.utils.ResultUtils;
import com.gp.pj.vo.UserVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Chloe
 * @since 2020-12-07
 */
@RestController
@RequestMapping("/")
public class LoginController {

    @Resource
    UserService userService;

    @ResponseBody
    @PostMapping(value = "/login")
    public Map<String,Object> login (@RequestBody JSONObject jsonRe){
        Map<String,Object> map = new HashMap<>();
        String userName = (String) jsonRe.get("userName");
        String password = (String) jsonRe.get("password");
        try{
            Map<String, UserVo> userMap = userService.login(userName ,password);
            System.out.println(userMap.toString());
            Map<String,Object> tokenMap = new HashMap<>();
            tokenMap.put("userId",userMap.get("userId"));
            tokenMap.put("userName",userMap.get("userName"));
            tokenMap.put("empId",userMap.get("empId"));
            tokenMap.put("empName",userMap.get("empName"));
            tokenMap.put("address",userMap.get("address"));
            tokenMap.put("stuNo",userMap.get("stuNo"));
            tokenMap.put("idCard",userMap.get("idCard"));
            tokenMap.put("gender",userMap.get("gender"));
            tokenMap.put("major","计算机科学与技术");
            tokenMap.put("gpa","0.0");
            String token = JWTUtils.getToken(tokenMap);
            System.out.println("token["+token+"]");
            map.put("msg","登录成功");
            map.put("token",token);
            map.put("code",0);
        }catch (Exception e){
            map.put("code",1);
            map.put("msg",e.getMessage());
        }
        return map;
    }

    @ResponseBody
    @GetMapping(value = "/get_info")
    public ResultUtils getUserInfo(String token){
        //获取用户信息
        try{
            JSONObject data = userService.getUserInfo(token);
            //将参数返回
            return ResultUtils.success(data);
        }catch (Exception e){
            return ResultUtils.failure("获取用户信息失败");
        }
    }

}
